package com.wonyesoft.wuliu.acticity.order;

import httptools.HttpJsonTool;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.OrderListBeen;
import javabeen.Row;

import org.json.JSONException;
import org.json.JSONObject;

import adapter.OrderListAdapter;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.AppContent.GetPushMessage;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.UserLoginActivity;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.api.GagApi;

/**
 * * @author 作者 陈修园:
 * 
 * @date 创建时间：2015-5-5 下午1:20:29
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class OrdersActivity extends BaseActivity {

	private TextView orderdetails_ing, orderdetails_finish,
			orderdetails_unfinished;
	private ViewPager viewPager;
	private List<View> pageViews;
	private TextView[] buttons;
	private int viewpageInde = 0;
	private LayoutInflater inflater;
	private View first;
	private View second;
	private View third;
	private String flowStatus;
	private List<OrderListBeen> sourceDateList;
	private ListView first_list_view;
	private ListView second_list_view;
	private ListView third_list_view;
	private boolean first_data = false;
	private boolean second_data = false;
	private boolean thred_data = false;
	private AppContent app;
	private List<OrderListBeen> first_get_data;
	private List<OrderListBeen> second_get_data;
	private List<OrderListBeen> thred_get_data;
	private OrderListAdapter first_adapter;
	private OrderListAdapter second_adapter;
	private OrderListAdapter third_adapter;
	private boolean Refreash = false;

	
	 TextView order_need_check;
	 TextView order_carry;
	 TextView order_complete;
	// private Button checkdata;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ly_orderdetails);
		Ini();
		initTitel(); 
		iniViewPage(); 
		IniListView();
		GetOrdersData();
		
		OnListenerPushMessage();
	}

	private void Ini() {
		orderdetails_ing = (TextView) findViewById(R.id.orderdetails_ing);
		orderdetails_finish = (TextView) findViewById(R.id.orderdetails_finish);
		orderdetails_unfinished = (TextView) findViewById(R.id.orderdetails_unfinished);
		orderdetails_ing.setBackgroundColor(Color.parseColor("#FFD700"));
		order_need_check=(TextView) findViewById(R.id.order_need_check);
		order_need_check.setOnClickListener(new GuideButtonClickListener(0));
		order_carry=(TextView) findViewById(R.id.order_carry);
		order_carry.setOnClickListener(new GuideButtonClickListener(1));
		order_complete=(TextView) findViewById(R.id.order_complete);
		order_complete.setOnClickListener(new GuideButtonClickListener(2));
		
		orderdetails_ing.setOnClickListener(new GuideButtonClickListener(0));
		orderdetails_finish.setOnClickListener(new GuideButtonClickListener(1));
		orderdetails_unfinished
				.setOnClickListener(new GuideButtonClickListener(2));
		viewPager = (ViewPager) findViewById(R.id.orderdetails_viewpager);
		buttons = new TextView[3];
		buttons[0] = orderdetails_ing;
		buttons[1] = orderdetails_finish;
		buttons[2] = orderdetails_unfinished;
		flowStatus = "1";
		app = (AppContent) getApplication();
		app.addActivity(this);
		sourceDateList = new ArrayList<OrderListBeen>();
		
		first_get_data = new ArrayList<OrderListBeen>();
		second_get_data = new ArrayList<OrderListBeen>();
		thred_get_data = new ArrayList<OrderListBeen>();
		ImageView reflesh = (ImageView) findViewById(R.id.head_right_img);
		reflesh.setImageResource(R.drawable.refresh);
		reflesh.setVisibility(View.VISIBLE);
		reflesh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				GetOrdersData();
			}
		});
	}

	private void iniViewPage() {
		inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		first = inflater.inflate(R.layout.orderlistview, null);
		second = inflater.inflate(R.layout.orderlistview, null);
		third = inflater.inflate(R.layout.orderlistview, null);
		pageViews.add(first);
		pageViews.add(second);
		pageViews.add(third);
		viewPager.setAdapter(new OrderViewPageAdapter());
		viewPager.setOnPageChangeListener(new OrderPageChangeListener());
	}

	private void GetOrdersData() {
		showLoadingDialog("加载中...");
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		if (app.userId == null && app.userinfobeen == null) {
			startActivity(new Intent(this, UserLoginActivity.class));
		}
		dataMap.put("cargoownerid", app.userId);

		dataMap.put("state", flowStatus);

		dataMap.put("state", flowStatus);


		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				dismissLoadingDialog();
				if (msg.what == 1) {
					List<OrderListBeen> data = (List<OrderListBeen>) msg.obj;
					IniListAdapter(data);
				} else {
					Toast.makeText(OrdersActivity.this, (String) msg.obj,
							Toast.LENGTH_LONG).show();
				}
			}
		});

		httpJsonTool.SetRowOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					Row row = (Row) msg.obj;
//					Toast.makeText(
//							OrdersActivity.this,
//									+ "", Toast.LENGTH_LONG).show();
				}
			}
		});

		Type type = new TypeToken<List<OrderListBeen>>() {
		}.getType();

		httpJsonTool.StartWork(dataMap, GagApi.GETPLANLIST, type, null);
	}

	

	
	private void IniListAdapter(List<OrderListBeen> data) {
		if (data != null) {
			if (viewpageInde == 0) {
				first_get_data.clear();
				for (int i = 0; i < data.size(); i++) {
					OrderListBeen d = data.get(i);
					first_get_data.add(d);
				}
				first_adapter = new OrderListAdapter(this, first_get_data,
						R.layout.activity_myorder_conent);
				first_list_view.setAdapter(first_adapter);
				first_list_view
						.setOnItemClickListener(new OrdersOnItemsClick());
				first_data = true;
			}

			if (viewpageInde == 1) {
				second_get_data.clear();
				for (int i = 0; i < data.size(); i++) {
					OrderListBeen d = data.get(i);
					second_get_data.add(d);
				}
				second_adapter = new OrderListAdapter(this, second_get_data,
						R.layout.activity_myorder_conent);
				second_list_view.setAdapter(second_adapter);
				second_list_view
						.setOnItemClickListener(new OrdersOnItemsClick());
				second_data = true;
			}

			if (viewpageInde == 2) {
				thred_get_data.clear();
				for (int i = 0; i < data.size(); i++) {
					OrderListBeen d = data.get(i);
					thred_get_data.add(d);
				}
				third_adapter = new OrderListAdapter(this, thred_get_data,
						R.layout.activity_myorder_conent);
				third_list_view.setAdapter(third_adapter);
				third_list_view
						.setOnItemClickListener(new OrdersOnItemsClick());
				thred_data = true;
			}
		}

	}

	private void IniListView() {
		first_list_view = (ListView) first.findViewById(R.id.listView1);
		second_list_view = (ListView) second.findViewById(R.id.listView1);
		third_list_view = (ListView) third.findViewById(R.id.listView1);
	}

	private void initTitel() {
		TextView title = (TextView) findViewById(R.id.head_title);
		title.setText("订单中心");
		ImageView imageView=(ImageView) findViewById(R.id.head_left);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				OrdersActivity.this.finish();
			}
		});
		ImageView backimage=(ImageView) findViewById(R.id.head_right_img);
		backimage.setVisibility(View.VISIBLE);
		backimage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			
				GetOrdersData();
			}
		});
	}

	private void OnListenerPushMessage() {

		app.SetPushMessageLister(new GetPushMessage() {

			@Override
			public void GetMessage(String msg, String CarTeamId,
					String CarTeamLeader) {
				// TODO Auto-generated method stub
				Refreash = true;
				// Log.v("sourceDateList", "" + sourceDateList.size());
				if (sourceDateList != null) {
					sourceDateList.clear();
					
					// Log.v("sourceDateList", "" + sourceDateList.size());
					GetOrdersData();
				}

				
			}
		});

	}

	
	class OrderViewPageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			// TODO Auto-generated method stub

			((ViewPager) container).removeView(pageViews.get(position));

		}

		@Override
		public Object instantiateItem(View container, int position) {
			// TODO Auto-generated method stub
			((ViewPager) container).addView(pageViews.get(position));
			return pageViews.get(position);
		}

	}

	class OrderPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			// Log.v("onPageSelected", "" + arg0);
			for (int i = 0; i < buttons.length; i++) {
				buttons[arg0].setBackgroundColor(Color.parseColor("#FFD700"));
				if (arg0 != i) {
					buttons[i].setBackgroundColor(Color.parseColor("#282722"));
				}
			}
			viewpageInde = arg0;
			if (arg0 == 0) {
				flowStatus = "1";
				if (!first_data) {
					// Log.v("onPageSelected", "" + arg0);
					GetOrdersData();
				}
			} else if (arg0 == 1) {
				flowStatus = "2";
				if (!second_data) {
					// Log.v("onPageSelected", "" + arg0);
					GetOrdersData();
				}
			} else {
				flowStatus = "3";
				if (!thred_data) {
					// Log.v("onPageSelected", "" + arg0);
					GetOrdersData();
				}
			}

		}

	}

	class GuideButtonClickListener implements OnClickListener {
		private int index = 0;

		public GuideButtonClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			viewPager.setCurrentItem(index, true);
		}
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.v("" + OrdersActivity.class.toString(), "onRestart");
		if (app.OrderActivity_data_has_change) {
			app.OrderActivity_data_has_change = true;
			GetOrdersData();
			if (viewpageInde == 0) {
				second_data = false;
				thred_data = false;
			}
			if (viewpageInde == 1) {
				first_data = false;

				thred_data = false;
			}
			if (viewpageInde == 2) {
				second_data = false;
				first_data = false;
			}
			// Log.v("" + OrdersActivity.class.toString(),
			// "OrderActivity_data_has_change");
			// Log.v("----OrderActivity_data_change_poition----",
			// app.OrderActivity_data_change_poition);
			// if (app.OrderActivity_data_change_poition.equals("1")) {
			//
			// if (viewpageInde == 0) {
			// GetOrdersData();
			// app.OrderActivity_data_change_poition = "0";
			// second_data = false;
			// } else {
			// first_data = false;
			// second_data = false;
			// }
			// if (viewpageInde == 1) {
			// GetOrdersData();
			// app.OrderActivity_data_change_poition = "0";
			// app.OrderActivity_data_has_change = false;
			// }
			// // GetOrdersData();
			// }
			// if (app.OrderActivity_data_change_poition.equals("3")) {
			// Log.v("----OrderActivity_data_change_poition------",
			// "------OrderActivity_data_change_poition--------");
			// if (viewpageInde == 1) {
			// GetOrdersData();
			// app.OrderActivity_data_change_poition = "0";
			// app.OrderActivity_data_has_change = false;
			// thred_data = false;
			// } else {
			// second_data = false;
			// thred_data = false;
			// }
			// if (viewpageInde == 2) {
			// GetOrdersData();
			// app.OrderActivity_data_change_poition = "0";
			// app.OrderActivity_data_has_change = false;
			// }
			// }

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		// showLoadingDialog("数据载入中...");
		Log.v("" + OrdersActivity.class.toString(), "onActivityResult");
		if (app.OrderActivity_data_change_poition != null) {
			if (app.OrderActivity_data_has_change) {

				if (app.OrderActivity_data_change_poition.equals("1")) {
					if ((first_get_data.size()>=resultCode)&&(first_get_data.size()>0)) {
						first_get_data.remove(resultCode);
					}
				
					//
					if (first_adapter != null) {
						first_adapter.notifyDataSetChanged();
					}

					Log.v("" + OrdersActivity.class.toString(),
							"first_get_data");
					second_data = false;
					first_data = false;
				}
				if (app.OrderActivity_data_change_poition.equals("3")) {
					Log.v("回来了", "这里是要刷新待确认和已完成");
					second_get_data.remove(resultCode);
					if (second_adapter != null) {
						second_adapter.notifyDataSetChanged();
					}

					Log.v("" + OrdersActivity.class.toString(),
							"second_get_data");
					thred_data = false;
					second_data = false;
				}
			}
		}
	}

	class OrdersOnItemsClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> listview, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Log.v("------------OrdersOnItemsClick---------------",
					"----------------OrdersOnItemsClick-------------------------");
			Log.v("点击的listview的总数:" + listview.getCount(), "点击的listview的总数:"
					+ listview.getCount());
			Intent intent = new Intent(OrdersActivity.this, OrderDetails.class);
			Bundle bundle = new Bundle();
			OrderListBeen data = (OrderListBeen) listview
					.getItemAtPosition(arg2);

			bundle.putInt("OrderId", data.getOrderformid());
			bundle.putInt("position", arg2);
			bundle.putString("State", flowStatus);
			intent.putExtras(bundle);
			startActivityForResult(intent, 3000001);
		}

	}

}
